package com.charging.service.impl;

import com.charging.dao.UserDao;
import com.charging.entity.PaymentRecord;
import com.charging.entity.User;
import com.charging.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User register(User user) {
        if (checkUsernameExist(user.getUsername())) {
            throw new RuntimeException("用户名已存在");
        }


        // 设置默认值
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setStatus(1); // 默认启用状态
        user.setBalance(0.0); // 默认余额为0
        user.setRole(0);// 默认角色为普通用户

        userDao.insert(user);
        return user;
    }

    @Override
    public User login(String username, String password) {
        User user = userDao.selectByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 简单密码验证（实际项目请使用加密验证）
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }

        if (user.getStatus() == 0) {
            throw new RuntimeException("账户已被禁用");
        }

        // 更新最后登录时间
        user.setLastLoginTime(new Date());
        userDao.update(user);

        return user;
    }

    @Override
    public User getUserById(Long id) {
        return userDao.selectById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.selectByUsername(username);
    }

    @Override
    public boolean updateUser(User user) {
        user.setUpdateTime(new Date());
        return userDao.update(user) > 0;
    }

    @Override
    public boolean addUser(User user) {
        return userDao.insert(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userDao.deleteById(id) > 0;
    }


    @Override
    public boolean checkUsernameExist(String username) {
        return userDao.selectByUsername(username) != null;
    }

    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public void updateUserLocation(Long userId, Double lat, Double lng) {
        userDao.updateUserLocation(userId, lat, lng);
    }

    @Override
    // 基础统计：总用户数
    public long getTotalUserCount() {
        return userDao.count();
    }

    @Override
    public User getUserByUserNameAndPassword(String username, String password) {
        return userDao.findByUserNameAndPassword(username, password);
    }

    @Override
    @Transactional
    public boolean updateBalance(Long userId, Double amount) {
        // 参数校验
        if (userId == null || amount == null || amount == 0) {
            throw new IllegalArgumentException("参数不能为空且金额不能为0");
        }

        // 查询用户
        User user = userDao.selectById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 计算新余额
        Double newBalance = user.getBalance() + amount;
        if (newBalance < 0) {
            throw new RuntimeException("余额不足");
        }

        // 更新用户余额
        user.setBalance(newBalance);
        int updateCount = userDao.updateById(user);
        if (updateCount <= 0) {
            throw new RuntimeException("更新余额失败");
        }

        return true;
    }

}